--We need to include the other files
AddCSLuaFile("cl_init.lua")
AddCSLuaFile("shared.lua")

include('shared.lua')

--This is what happens when the entity is created
function ENT:SpawnFunction( ply, tr )

	if ( !tr.Hit ) then return end
	
	local SpawnPos = tr.HitPos + tr.HitNormal * 16
	
	local ent = ents.Create( "sent_ball" )
		ent:SetPos( SpawnPos )
	ent:Spawn()
	ent:Activate()
	
	return ent
	
end

--The initialization of the ent
function ENT:Initialize()
	--Set the model of the ent
	self:SetModel("models/Dodgeball.mdl")
	--Set the physics type
	self:PhysicsInit(SOLID_VPHYSICS)
	self.Entity:SetMoveType(MOVETYPE_VPHYSICS)
	self.Entity:SetSolid(SOLID_VPHYSICS)
	local phys = self.Entity:GetPhysicsObject()
	self.NextThink = CurTime() + 1
	self.IsDisabled = false
	self.Entity:DrawShadow(true)
	--Enable the physics
	if (phys:IsValid()) then
		phys:Wake()
		phys:SetMass(10)
	end
	--Load some sounds for the ent to make
	self.Hit = 	Sound("Rubber.BulletImpact");

	self.FleshHit = Sound("Rubber.BulletImpact");
	--print (self:GetOwner():Nick())
	--self:GetPhysicsObject():SetMass(40)	

	//self.Entity:SetUseType(SIMPLE_USE)
end

function ENT:Think()
end

/*---------------------------------------------------------
   Name: ENT:PhysicsCollided() // I have hit somthing, what is it?
---------------------------------------------------------*/
function ENT:PhysicsCollide(data, phys)
	
	local Ent = data.HitEntity
	if !(ValidEntity(Ent) or Ent:IsWorld()) then return end
	--Lets see what i hit
	if Ent:IsWorld() then
		--If i hit the world, make some noise
		//util.Decal("ManhackCut", data.HitPos + data.HitNormal, data.HitPos - data.HitNormal)
		self:EmitSound(self.Hit)
	elseif Ent.Health then
		--If i hit something with health
		if not(Ent:IsPlayer() or Ent:IsNPC() or Ent:GetClass() == "prop_ragdoll") then 
			--if it is not either a player or an NPC or if it is a ragdoll make some noise
			self:EmitSound(self.Hit)
		else
			--Otherwise we want to make a blood impact effect and play the flesh hit sound
			//if (Ent:IsPlayer() or Ent:IsNPC() or Ent:GetClass() == "prop_ragdoll") then 
			local effectdata = EffectData()
			effectdata:SetStart(data.HitPos)
			effectdata:SetOrigin(data.HitPos)
			effectdata:SetScale(1)
			util.Effect("BloodImpact", effectdata)
			self:EmitSound(self.FleshHit)
		end
		--Apply some damage to whatever it hit
		if (Ent:IsPlayer()) then
			if (Ent:Team() == self:GetOwner():Team()) then
				--Do nothing.
			else
				Ent:TakeDamage(9000, self:GetOwner())
			end
		else
			Ent:TakeDamage(9000, self:GetOwner())
		end
	end
	--Disable the entity and set it's owner to noone
	self:Disable()
	self.Entity:SetOwner(NUL)
	--Create the disabled ball entity
end

--When a player presses the E button
function ENT:Use(activator, caller)
	--Make sure the team they are on is valid
	if (activator:Team() != TEAM_SPECTATOR and activator:Team() != 3 and activator:Team() != 4) then
		--Check if the ball entity is disabled
		if (self.IsDisabled != false) then
			self.Entity:Remove()
			--Get who is using me
			if (activator:IsPlayer()) then
				--if they do not have the throwing arm weapon then give them one
				if activator:GetWeapon("weapon_throwingarm") == NULL then
					activator:Give("weapon_throwingarm")
				else
					--Otherwise just give them some ammo
					activator:GiveAmmo(1, "Grenade")
				end
			end
		else
			--The player only has a chance to catch the ball(I HOPE)
			if (math.random(0, 100) == 0) then
				self.Entity:Remove( )
				--Kill the owner because the other player caught it.
				if ( self:GetOwner( ):IsPlayer( ) ) then
					self:GetOwner( ):Kill( )
				end
				--Get who is using me
				if (activator:IsPlayer()) then
					--if they do not have the throwing arm weapon then give them one
					if activator:GetWeapon("weapon_throwingarm") == NULL then
						activator:Give("weapon_throwingarm")
					else
						--Otherwise just give them some ammo
						activator:GiveAmmo(1, "Grenade")
					end
				end
			end
		end
	end
end

--What happens when the ent is disabled
function ENT:Disable()
	self.IsDisabled = true
	self.PhysicsCollide = function() end
	self:GetPhysicsObject():SetMass(1)
end